package com.link.base.base.report.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.link.base.base.report.model.*;
import com.link.base.base.report.model.*;
import com.link.base.base.report.service.StoreReportService;
import com.link.base.user.model.CoreUser;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.util.RedisUtil;
import com.link.core.util.UserUtil;
import com.link.core.util.redisclient.LinkRedisClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 门店报表
 *
 * @author unknown
 * @date unknown
 */
@Controller
@RequestMapping("/link/report")
public class StoreReportController {
    @Resource
    private StoreReportService storeReportService;

    /**
     * 门店首页报表-产品销售汇总报表
     * 统计一定时间内的采购、采购退货、销售、销售退货的数量和金额
     * YXL
     */
    @RequestMapping(value = "/queryStoreProdSale", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryStoreProdSale(@JsonParam ProdSaleStore entity, @JsonParam QueryParams qps) {
        Map<String,Object> result = new HashMap<>();
        try {
            entity.setPageFlag(false);
            entity = (ProdSaleStore) BasicModel.transformClass(entity, qps);
            if (entity.getQps() != null) {
                entity.getQps().getFilters().forEach(f -> {
                    f.setProperty(f.getProperty().replace("`", ""));
                });
            }
            List<ProdSaleStore> list = storeReportService.queryStoreProdSale(entity);
            result.put("success", true);
            result.put("rows", list);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 门店首页报表-店员销售报表
     * 统计门店一定时间内各店员销售产品的数量和金额
     * YXL
     */
    @RequestMapping(value = "/queryStoreSalemanProdSale", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryStoreSalemanProdSale(@JsonParam ProdSalemanSale entity, @JsonParam QueryParams qps) {
        Map<String,Object> result = new HashMap<>();
        try {
            entity.setPageFlag(false);
            entity = (ProdSalemanSale) BasicModel.transformClass(entity, qps);
            if (entity.getQps() != null) {
                entity.getQps().getFilters().forEach(f -> {
                    f.setProperty(f.getProperty().replace("`", ""));
                });
            }
            List<ProdSalemanSale> list = storeReportService.queryStoreSalemanProdSale(entity);
            result.put("success", true);
            result.put("rows", list);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 门店首页报表-产品库存汇总报表
     * 统计一定时间内门店产品的期初成品库存量、成品出库、成品入库、期末成品库存、期初破损库存、破损入库、破损出库、期末破埙库存
     * YXL
     */
    @RequestMapping(value = "/queryStoreInvtxn", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryStoreInvtxn(@JsonParam StoreInvtxn entity, @JsonParam QueryParams qps) {
        Map<String,Object> result = new HashMap<>();
        try {
            entity.setPageFlag(false);
            entity = (StoreInvtxn) BasicModel.transformClass(entity, qps);
            if (entity.getQps() != null) {
                entity.getQps().getFilters().forEach(f -> {
                    f.setProperty(f.getProperty().replace("`", ""));
                });
            }
            List<StoreInvtxn> list = storeReportService.queryStoreInvTxn(entity);
            result.put("success", true);
            result.put("rows", list);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 门店首页报表-订单销售付款报表
     * 统计一定时间内门店的销售金额、各渠道付款金额（支付宝、微信、银联、现金）及开票金额
     * YXL
     */
    @RequestMapping(value = "/queryStoreOrderPay", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryStoreOrderPay(@JsonParam StoreOrderPay entity, @JsonParam QueryParams qps) {
        Map<String,Object> result = new HashMap<>();
        try {
            entity.setPageFlag(false);
            entity = (StoreOrderPay) BasicModel.transformClass(entity, qps);
            if (entity.getQps() != null) {
                entity.getQps().getFilters().forEach(f -> {
                    f.setProperty(f.getProperty().replace("`", ""));
                });
            }
            List<StoreOrderPay> list = storeReportService.queryStoreOrderPay(entity);
            result.put("success", true);
            result.put("rows", list);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 门店首页报表-开票明细
     * 统计一定时间内门店开票记录信息
     * YXL
     */
    @RequestMapping(value = "/queryStoreInvoice", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryStoreInvoice(@JsonParam StoreInvoice entity, @JsonParam QueryParams qps) {
        Map<String,Object> result = new HashMap<>();
        try {
            entity.setPageFlag(false);
            entity = (StoreInvoice) BasicModel.transformClass(entity, qps);
            if (entity.getQps() != null) {
                entity.getQps().getFilters().forEach(f -> {
                    f.setProperty(f.getProperty().replace("`", ""));
                });
            }
            List<StoreInvoice> list = storeReportService.queryStoreInvoice(entity);
            result.put("success", true);
            result.put("rows", list);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 门店首页-金额统计
     * 统计门店当天的采购金额、销售金额、付款金额、开票金额
     * YXL
     */
    @RequestMapping(value = "/queryStoreHomeAmount", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryStoreHomeAmount(@JsonParam StoreHomeAmount entity, @JsonParam QueryParams qps) {
        Map<String,Object> result = new HashMap<>();
        try {
            entity.setPageFlag(false);
            entity = (StoreHomeAmount) BasicModel.transformClass(entity, qps);
            List<StoreHomeAmount> list = storeReportService.queryStoreHomeAmount(entity);
            result.put("success", true);
            result.put("rows", list);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**
     * 门店-wifi设备统计
     * YXL
     */
    @RequestMapping(value = "/queryDayWifiNum", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryDayWiFiNum(@JsonParam WifiNum entity, @JsonParam QueryParams qps) {
        Map<String,Object> result = new HashMap<>();
        //从redis查询是否有缓存数据
        LinkRedisClient jedis = RedisUtil.getJedis();
        try {
            CoreUser user = UserUtil.getUser();
            List<WifiNum> list = new ArrayList<WifiNum>();
            String key = "lnk_data_task_pc_" + user.getUsername();

            if ( jedis.exists( key )) {
                String listStr = jedis.get(key);
                list = JSON.parseObject(listStr, new TypeReference<List<WifiNum>>() {});
            } else {
                entity.setPageFlag(false);
                list = storeReportService.queryDayWifiNum(entity);
                String jsonStr = JSON.toJSONString(list);
                jedis.del(key);
                jedis.set(key,jsonStr);
                jedis.expire(key,300);
            }
            result.put("success", true);
            result.put("rows", list);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        } finally {
            RedisUtil.returnResource(jedis);
        }
        return result;
    }
}
